<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;
            position: absolute;
            left: 0;
            top: 0;
            background: palevioletred;
        }
        div h4 {
            width: 200px;
            height: 40px;
            background: palegreen;
        }
    </style>
</head>
<body>
    <div id="box">box</div>
    <div id="box1">
        <h4 class="title">box1</h4>
    </div>
    <script>
        class Drag {
            constructor (selector,titleSelect) { 
                this.box = document.querySelector(selector)
                this.title = titleSelect ? this.box.querySelector(titleSelect) : this.box
                this.bindEvent()
            }
            bindEvent(){
                this.title.onmousedown = (e) =>{
                    let disx = e.offsetX
                    let disy = e.offsetY
                    document.onmousemove = (e) =>{
                        let left = e.clientX - disx
                        let top = e.clientY - disy
                        this.move(left,top)
                    }
                    document.onmouseup = () =>{
                        document.onmousemove = null
                    }
                    return false
                }
            }
            move(left,top){
                this.box.style.left = left + 'px'
                this.box.style.top = top + 'px'
            }

        }
        new Drag('#box')
        new Drag('#box1' , '.title')
    </script>
</body>
</html>